﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Spark.AspNetCore;
using Spark.Swagger;
using Swashbuckle.AspNetCore.Swagger;

//using Spark.LoadBalancer.Extensions;
//using Spark.ServiceDiscovery.Extensions;
//using Spark.ServiceDiscovery.Remote.Extensions;

namespace Spark.Samples.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSpark(builder =>
            {
                builder
                    .AddSwagger(
                        option =>
                        {
                            option.Add("v1",
                                new Info
                                {
                                    Title = "User.API",
                                    Version = "v1",
                                    Description = "https://www.91guodaxia.com/"
                                });
                        }, "Spark.Samples.WebApi");
                //添加认证
                //.AddAuthentication(Configuration)
                //添加SmarkSql数据库支持
                //.AddSmartSql()
                //添加消息总线
                //.AddEventBus(x => x.UseRabbitMQ(Configuration))
                //负载均衡
                //.AddLoadBalancer()
                //.AddCsRedisCache(x => x.ConnectionString = "192.168.0.236:6379,password=123456,connectTimeout=2000,DefaultDatabase=8")
                //服务发现
                //.AddServiceDiscovery(x => x.UseRemote(Configuration));
                //添加分布式日志
                //.AddLog(x => x.UseEventBusLog(Configuration));
                //添加检索引擎
                //.AddElasticesearch(Configuration)
                //添加链路追踪
                //.AddTracer(Configuration)
                //添加健康检查
                //.AddHealthChecks(x =>
                //{
                //    x.AddSqlCheck("spark", "dd");
                //});

                builder.AddMvc(CompatibilityVersion.Version_2_1);
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.Use(async (context, next) =>
            //{
            //    context.Request.EnableRewind();
            //    await next();
            //});

            //app.UseSparkLog();
            //.UseGlobalErrorMiddleware()
            //.UseHttpMethodMiddleware()
            //.UseStatisticsMiddleware();
            //.UseAuthentication();

            app.UseSpark()
                .UseSwagger()
                .UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Spark.Samples.WebApi"); });

            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}